var http = require('http');
var url = require('url');
var util = require('util');
var fs = require('fs');


// node jQuery
var cheerio = require('cheerio');
// http request
var superagent = require('superagent');

var req = http.request({
    host: 'www.lesmao.com'
}, res => {
    var indexContent = '';

    res.on('data', data => indexContent += data);
    res.on('end', () => {
        var pageUrls = getPageUrls(indexContent);
        //log(pageUrls);
        getPage(pageUrls[0]);
    });
    res.on('error', err => console.log('err'));
});

req.end();

function saveHtml(html, fileName = 'index.html') {
    var writeStream = fs.createWriteStream(fileName);
    writeStream.write(html, 'UTF8');
    writeStream.end();
}


function parseUrl2Options(oriUrl) {
//    var urlObj = new url.URL(oriUrl);
    var urlObj = url.parse(oriUrl);
    var options = {
        host: urlObj.host,
        path: urlObj.pathname
    };
    return options;
}

function getPage(pageUrl) {
    var options = parseUrl2Options(pageUrl);
    console.log('options:');
    log(options);
    var req = http.request(options, res => {
        var html = '';
        res.on('data', data => html += data);
        res.on('end', () => {
            console.log('page end...');
            //saveHtml(html, 'page.html');
            var picUrls = getPicUrls(html);
            log(picUrls);

            savePic(picUrls[0]);
        });
    });
    req.end();
}

function log(obj) {
    console.log(util.inspect(obj, {
        colors: true
    }));
}


function getPageUrls(indexContent) {
    var pageUrls = []
    var urlExp = /<div class="photo"><a href="(.+html)?"(\stitle=".+")?\starget/g;
    var result = urlExp.exec(indexContent);
    while(result) {
        pageUrls.push(result[1]);
        result = urlExp.exec(indexContent);
    }
    return pageUrls;
}


function getPicUrls(pageContent) {
    var picUrls = [];
    var picExp = /<li><img\salt=".+"\ssrc="(.+jpg)"\s/g;
    var result = picExp.exec(pageContent);
    while(result) {
        picUrls.push(result[1]);
        result = picExp.exec(pageContent);
    }
    return picUrls;
}


function savePic(picUrl) {
    var options = parseUrl2Options(picUrl);
    http.get(options, res => {
        var imgData = '';
        res.setEncoding('binary');
        res.on('data', buffer => {
            imgData += buffer;
        });
        res.on('end', () => {
            fs.writeFile('mypic.jpg', imgData, 'binary', err => {
                if(err) {
                    return console.log('save pic falid..');
                }
                console.log('save pic success...');
            });
        });
    });
}
